<template>
    <div class="qilin-promotionDialog">
        <el-dialog
            :visible.sync="ownDialogConfig.isShow" :title="ownDialogConfig.title"
            width="600px" center append-to-body :before-close="handleClose"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-form ref="formData" :model="formData" :rules="formRule" size="mini"
                label-width="120px" label-position="right" label-suffix=":" class="upload-form"
            >
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="部门" prop="employeeDepartment">
                            <el-select placeholder="请选择" v-model="formData.employeeDepartment"
                                clearable disabled
                            >
                                <el-option v-for="item in employeeDepartmentList" :key="item.id"
                                    :label="item.label" :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="姓名" prop="employeeId">
                            <el-select placeholder="请选择姓名" v-model="formData.employeeId" clearable
                                filterable disabled
                            >
                                <el-option v-for="item in employeeNameList" :key="item.id"
                                    :label="`${item.label}(${item.code})`" :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="12">
                        <el-form-item label="选择年份" prop="year">
                            <el-select placeholder="请选择年份" v-model="formData.year" clearable>
                                <el-option v-for="item in yearList" :key="item.value"
                                    :label="item.label" :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="12">
                        <el-form-item label="绩效积分" prop="performanceIntegral">
                            <el-input placeholder="请输入绩效积分" v-model.number="formData.performanceIntegral"
                                type="number" @blur="calculateScore"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="资质积分" prop="certificationIntegral">
                            <el-input placeholder="请输入资质积分" v-model.number="formData.certificationIntegral"
                                type="number" @blur="calculateScore"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="创新积分" prop="innovateIntegral">
                            <el-input placeholder="请输入创新积分" v-model.number="formData.innovateIntegral"
                                type="number" @blur="calculateScore"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="总积分" prop="aggregateScore">
                            <el-input placeholder="请输入总积分" v-model="formData.aggregateScore" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="P职级" prop="prank">
                            <el-input placeholder="请输入P职级" v-model="formData.prank" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="基础职级调整" prop="rankAdjust">
                            <el-select placeholder="请选择基础职级调整" v-model="formData.rankAdjust" disabled>
                                <el-option label="是" :value="1"></el-option>
                                <el-option label="否" :value="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="调整后基础职级" prop="adjustBaseRank">
                            <el-input placeholder="请输入调整后基础职级" v-model="formData.adjustBaseRank" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="adjustScore">
                            <div slot="label">
                                <el-tooltip placement="left" effect="dark">
                                    <div slot="content">基础职级调整后积分</div>
                                    <span class="ellipsis">基础职级调整后积分</span>
                                </el-tooltip>
                            </div>
                            <el-input placeholder="请输入基础职级调整后积分" v-model="formData.adjustScore" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="调整后P职级" prop="adjustPRank">
                            <el-input placeholder="请输入调整后P职级" v-model="formData.adjustPRank" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="surplusPerformanceIntegral">
                            <div slot="label">
                                <el-tooltip placement="left" effect="dark">
                                    <div slot="content">（基础职级和P职级）调整后剩余绩效积分</div>
                                    <span class="ellipsis">（基础职级和P职级）调整后剩余绩效积分</span>
                                </el-tooltip>
                            </div>
                            <el-input placeholder="请输入（基础职级和P职级）调整后剩余绩效积分"
                                v-model="formData.surplusPerformanceIntegral" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer">
                <el-button type="default" size="mini" @click="handleClose">取消</el-button>
                <el-button type="primary" size="mini" @click="submitData('formData')">确认</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {getEmployeeData,updatePromotionData} from "@/api/index.js";
export default {
    data(){
        return {
            ownDialogConfig:{ //弹窗自身的配置
                isShow:false,
                title:"编辑"
            },
            formData:{ //表单配置项
                employeeDepartment:"",
                employeeName:"",
                employeeId:"",
                // year:"",
                performanceIntegral:"",
                certificationIntegral:"",
                innovateIntegral:"",
                aggregateScore:"",
                prank:"",
                rankAdjust:"",
                adjustBaseRank:"",
                adjustScore:"",
                adjustPRank:"",
                surplusPerformanceIntegral:""
            },
            formRule:{ //表单规则配置项

            },
            employeeDepartmentList:[ //部门下拉选择数据集
                {
                    label:"综合部",
                    value:"综合部",
                    id:1
                },
                {
                    label:"财务部",
                    value:"财务部",
                    id:2
                },
                {
                    label:"市场拓展部",
                    value:"市场拓展部",
                    id:3
                },
                {
                    label:"系统集成部",
                    value:"系统集成部",
                    id:4
                },
                {
                    label:"工程部",
                    value:"工程部",
                    id:5
                },
                {
                    label:"产品研发部",
                    value:"产品研发部",
                    id:6
                },
                {
                    label:"平台运维部",
                    value:"平台运维部",
                    id:7
                }
            ],
            employeeNameList:[], //姓名下拉选择数据集
            yearList:[] //年份下拉选择数据集
        }
    },
    methods:{
        // 修改时事件监听
        initUpdate(data){
            data.yearList.forEach((item)=>{
                if(item.year == new Date().getFullYear()){
                    for(let key in item){
                        this.formData[key]=item[key] ?? 0;
                    }
                }
            });
            this.formData.employeeDepartment=data.employeeDepartment;
            this.formData.employeeName=data.employeeName;
            this.formData.employeeId=data.employeeId;
            this.formData.employeeCode=data.employeeCode;
            this.getEmployeeData(this.formData.employeeDepartment);
            this.ownDialogConfig.isShow=true;
        },
        // 初始化获取所在部门下人员信息事件监听
        getEmployeeData(departmentName){
            getEmployeeData({employeeDepartment:departmentName}).then((res)=>{
                if(res.code === 200){
                    this.employeeNameList=res.result.map((item)=>{
                        return {
                            id:item.employeeId,
                            label:item.employeeName,
                            value:item.employeeId,
                            code:item.employeeCode
                        }
                    });
                }
            });
        },
        // 当绩效积分、资质积分、创新积分失去焦点时计算事件监听
        calculateScore(){
            // console.log(this.formData.performanceIntegral);
            if(this.formData.rankAdjust && this.formData.rankAdjust == 1){
                const performanceIntegral=this.formData.performanceIntegral?this.formData.performanceIntegral:0;
                const certificationIntegral=this.formData.certificationIntegral?this.formData.certificationIntegral:0;
                const innovateIntegral=this.formData.innovateIntegral?this.formData.innovateIntegral:0;
                const totalScore=performanceIntegral+certificationIntegral+innovateIntegral;
                this.formData.surplusPerformanceIntegral=this.formData.adjustScore=this.formData.aggregateScore=
                    totalScore < 120 ? totalScore : (totalScore - 120);
                this.formData.adjustBaseRank=this.formData.adjustPRank=totalScore < 120 ? this.formData.prank :
                    ((this.formData.prank < 12) ? (this.formData.prank +1) : this.formData.prank);
                // this.formData.adjustBaseRank=totalScore < 120 ? this.formData.prank :
                //     ((this.formData.prank < 12) ? (this.formData.prank +1) : this.formData.prank);
                this.formData.rankAdjust=totalScore < 120 ? (this.formData.rankAdjust ? 1 : 0) : 1;
            }
        },
        // 点击确认按钮事件监听
        submitData(refName){
            this.calculateScore();
            this.$refs[refName].validate((valid)=>{
                if(valid){
                    let formData={
                        employeeDepartment:this.formData.employeeDepartment,
                        employeeId:this.formData.employeeId,
                        employeeName:this.formData.employeeName,
                        employeeCode:this.formData.employeeCode,
                        performanceIntegral:this.formData.performanceIntegral,
                        certificationIntegral:this.formData.certificationIntegral,
                        innovateIntegral:this.formData.innovateIntegral,
                        aggregateScore:this.formData.aggregateScore,
                        prank:this.formData.prank,
                        rankAdjust:this.formData.rankAdjust,
                        adjustBaseRank:this.formData.adjustBaseRank,
                        surplusPerformanceIntegral:this.formData.surplusPerformanceIntegral,
                        year:this.formData.year
                    };
                    updatePromotionData(formData).then((res)=>{
                        // console.log(res);
                        if(res.code === 200){
                            this.$message({
                                message:"编辑成功",
                                type:"success"
                            });
                            this.$emit("refresh");
                            this.handleClose();
                        }else{
                            this.$message({
                                message:res.message || "请求错误",
                                type:"error"
                            });
                        }
                    });
                }
            });
        },
        // 关闭弹窗事件监听
        handleClose(){
            this.$refs["formData"].resetFields();
            for(let key in this.formData){
                this.formData[key]="";
            }
            this.ownDialogConfig.isShow=false;
        }
    }
}
</script>
<style lang="scss" scoped>

</style>
